<?

/**
* insert into detail
*
* @param  string  $COracle
* @param  string  $values
* @param  string  $srcDb
*
*/ 
function insToDbDtl(&$COracle,
                    &$values,
                    &$srcDb){

  $query  = "INSERT INTO ".$srcDb." 
             (JOB_ID,
              NO,
              MODEL,
              QTY,
              SER_NUMBER,
              REASON)
             VALUES (".$values.") ";
  
  $COracle->execQuery($query);
}

/**
* insert into header
* 
* @param  string  $COracle
* @param  string  $values
* @param  string  $srcDb
*
*/
function insToDbHdr(&$COracle,
                    &$values,
                    &$srcDb){

  $query  = "INSERT INTO ".$srcDb." 
             (JOB_ID,
              REFERENCE,
              TR_DATE,
              NO,
              STATUS)
             VALUES (".$values.") ";
  
  $COracle->execQuery($query);
}

/**
* insert into model input history
*
* @param  string  $COracle
* @param  string  $values
* @param  string  $srcDb
*
*/ 
function insTxHistory(&$COracle,
                      &$values,
                      &$srcDb){
  
  $query  = "INSERT INTO ".$srcDb." 
             (JOB_ID,
              TR_DATE,
              RECV_REF,
              ICAT,
              IGROUP,
              MODEL,
              MODEL_ID,
              UNIT,
              QTY,
              RECV_QTY,
              SER_NUMBER,
              PRICE,
              HRZ_TOT,
              DISC_PCTG,
              DISC,
              NETTO,
              DTL_NO,
              NO,
              TX_TYPE,
              STATUS)
             VALUES (".$values.") ";
             
  $COracle->execQuery($query);
}

/**
* update inventory balance
*
* @param  string  $COracle
* @param  string  $model
* @param  string  $modelId
* @param  string  $unit
* @param  integer $number
* @param  string  $srcDb
*
*/
function updateInvBalance(&$COracle,
                          &$model,
                          &$modelId,
                          &$unit,
                          $number,
                          &$srcDb){
  
  fetchInvBalance($COracle,
                  $model,
                  $modelId,
                  $unit,
                  $srcDb,
                  $qty);
  
  if(!$qty){
    
    usrDieJs("Error: Data corrupted, Invalid balance");
  }else{
    
    $qty += $number;
  }
  
  if($modelId != "" && $unit != ""){
  
    $query = "UPDATE ".$srcDb." 
              SET QTY=".$qty." 
              WHERE MODEL='".$model."' 
                    AND MODEL_ID=".$modelId."
                    AND UNIT='".$unit."' ";
  }else{
    
    $query = "UPDATE ".$srcDb." 
              SET QTY=".$qty." 
              WHERE MODEL='".$model."' ";
  }
                    
  $COracle->execQuery($query);
}

/**
* Insert the header of this manual serial number input
*
* @param  string  $COracle
* @param  string  $thisJid
* @param  integer $srcDtlNo
* @param  integer $srcSubNo
* @param  string  $srcDb
*
*/
function insMiniInputHdr( &$COracle,
                          &$thisJid,
                          &$srcDtlNo,
                          &$srcSubNo,
                          &$srcDb){

  $values = " '".$thisJid."',   ".
            "  ".$srcDtlNo." ,  ".
            "  ".$srcSubNo."    ";

  $query  = "INSERT INTO ".$srcDb." 
             (JOB_ID,
              DTL_NO,
              SUB_NO)
             VALUES (".$values.") ";
             
  $COracle->execQuery($query);
}        

/**
* Insert the header of this manual serial number input
*
* @param  string  $COracle
* @param  string  $thisJid
* @param  string  $serNumber
* @param  integer $srcDb
*
*/
function insMiniInputDtl( &$COracle,
                          &$thisJobId,
                          $serNumber,
                          &$srcDb){

  CGFetchers::fetchLastNumberByJobId($COracle,
                                     $thisJobId,
                                     $srcDb,
                                     $lastNumber);
  
  $values = " '".$thisJobId."', ".
            " '".$serNumber."', ".
            "  ".$lastNumber."  ";
  
  $query  = "INSERT INTO ".$srcDb."
             (JOB_ID,
              SER_NUMBER,
              NO)
             VALUES (".$values.") ";
             
  $COracle->execQuery($query);
}

/**
* Update the model history entry
*
* @param  string  $COracle
* @param  string  $srcSerNumber
* @param  string  $txType
* @param  string  $srcDb
*
*/
function updateModelHistory(&$COracle,
                            &$srcSerNumber,
                             $txType,
                            &$srcDb){

  $query  = "UPDATE ".$srcDb."
             SET TX_TYPE=".$txType." 
             WHERE  SER_NUMBER='".$srcSerNumber."' ";
             
  $COracle->execQuery($query);
}

/**
* update the model's input history
*
* @param  string  $COracle
* @param  string  $baseStatus
* @param  string  $txStatus
* @param  string  $jobId
* @param  integer $txType
* @param  string  $srcDb
*
*/
function updateInputHistory(&$COracle,
                            &$baseStatus,
                            &$txStatus,
                            &$jobId,
                            $txType,
                            &$srcDb){

  $query  = "UPDATE ".$srcDb."
             SET  STATUS='".$txStatus."'
             WHERE  JOB_ID='".$jobId."'
                    AND STATUS='".$baseStatus."'
                    AND TX_TYPE=".$txType." ";
                    
  $COracle->execQuery($query);
}

/**
* insert into this model name and model id history
* 
* @param  string  $COracle
* @param  string  $values
* @param  integer $txType
* @param  string  $srcDb
*
*/
function insToHistory( &$COracle,
                       &$values,
                       &$txType,
                       &$srcDb){

  $values .= ",".$txType." ";
  
  $query  = "INSERT INTO ".$srcDb." 
             (NO,
              TR_DATE,
              SRC_REF,
              SRC_NO,
              MODEL,
              MODEL_ID,
              UNIT,
              SER_NUMBER,
              TX_TYPE)
             VALUES (".$values.") ";
             
  $COracle->execQuery($query);
}

/**
* update the manual input SRC_REF 
* since the previous is Invalid
*
* @param  string  $COracle
* @param  string  $serNumber
* @param  string  $jobId
* @param  string  $srcDb
*
*/
function updateManInSrcRef( &$COracle,
                            &$serNumber,
                            &$jobId,
                            &$srcDb){
  
  /**
  * make sure it is _MANUAL_KEY_IN
  * YES: process the update with the $theSerial[_2ND] as the 
  *      manual key in JobId
  * NO : Do nothing
  *
  */
  $theSerial = explode(_DEFAULT_SEPARATOR_02,
                       $serNumber);
                       
  if($theSerial[_1ST] == _MANUAL_KEY_IN){
  
    $query  = "UPDATE ".$srcDb." 
               SET SRC_REF='".$jobId."' 
               WHERE JOB_ID='".$theSerial[_2ND]."' ";
               
    $COracle->execQuery($query);
  }else{
    
    /* Do nothing */
  }
}
?>